<template>
  <div class="mod-user">
    <el-form :inline="true" :model="searchForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="searchForm.nickname" clearable placeholder="请输入昵称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="searchForm.mobile" clearable placeholder="请输入手机号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
<!--        <el-button v-if="isAuth('mall:user:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>-->
        <!--        <el-button v-if="isAuth('mall:user:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量冻结</el-button>-->
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      style="width: 100%;"
      @selection-change="selectionChangeHandle">
      <el-table-column
        align="center"
        header-align="center"
        type="selection"
        width="50">
      </el-table-column>
      <el-table-column
        :show-overflow-tooltip="true"
        align="center"
        header-align="center"
        label="昵称"
        prop="nickname">
      </el-table-column>
      <el-table-column
        :show-overflow-tooltip="true"
        align="center"
        header-align="center"
        label="手机号"
        prop="mobile">
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label="性别"
        prop="genderName">
      </el-table-column>
      <el-table-column
        :show-overflow-tooltip="true"
        align="center"
        header-align="center"
        label="微信"
        prop="openId">
      </el-table-column>
      <el-table-column
        :show-overflow-tooltip="true"
        align="center"
        header-align="center"
        label="注册时间"
        prop="registerTime">
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label="最后登录时间"
        prop="lastLoginTime">
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label="关注状态"
        prop="subscribe">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.subscribe === 1" size="small" type="success">已关注</el-tag>
          <el-tag v-else size="small" type="danger">未关注</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label="是否老师"
        prop="sysUserId">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.sysUserId" size="small" type="danger">是</el-tag>
          <el-tag v-else size="small" type="success">否</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label="管理账号"
        prop="sysUsername">
      </el-table-column>
      <el-table-column
        align="center"
        fixed="right"
        header-align="center"
        label="操作"
        width="150">
        <template slot-scope="scope">
          <el-button v-if="isAuth('mall:user:info')" size="small" type="text" @click="showDetails(scope.row.id)">查看
          </el-button>
          <el-button v-if="isAuth('mall:user:info')" size="small" type="text" @click="translateHandle(scope.row.id)">
            律师授权
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="pageIndex"
      :page-size="pageSize"
      :page-sizes="[10, 20, 50, 100]"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <!-- 管理员授权 -->
    <add-manager v-if="addManagerVisible" ref="addManager" @refreshDataList="getDataList"></add-manager>

  </div>
</template>

<script>
import AddOrUpdate from './user-add-or-update'
import AddManager from './user-add-manager'

export default {
  data() {
    return {
      searchForm: {
        mobile: '',
        realname: '',
        nickname: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListSelections: [],
      addOrUpdateVisible: false,
      addManagerVisible: false
    }
  },
  components: {
    AddOrUpdate,
    AddManager
  },
  activated() {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList() {
      if (this.$route.query.id) {
        this.searchForm.id = this.$route.query.id
      }
      this.$http({
        url: '/mall/user/list',
        method: 'get',
        params: {
          'page': this.pageIndex,
          'limit': this.pageSize,
          'nickname': this.searchForm.nickname,
          'mobile': this.searchForm.mobile,
          'id': this.searchForm.id
        }
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.dataList = data.page.records
          this.totalPage = data.page.total
        } else {
          this.dataList = []
          this.totalPage = 0
        }
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 查看详情
    showDetails(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id, true)
      })
    },
    // 授权
    translateHandle(mallUserId, sysUserId) {
      if (sysUserId) {
        this.$message({
          message: '该用户已是老师角色，无需再次授权。若需修改管理员信息请到[管理员列表]功能',
          type: 'warning',
          duration: 1500
        })
        return
      }

      this.addManagerVisible = true
      this.$nextTick(() => {
        this.$refs.addManager.init(mallUserId, sysUserId)
      })
    },
    // 删除
    deleteHandle(id) {
      let ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定进行[冻结]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: '/mall/user/delete',
          method: 'post',
          data: ids
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500
            })
            this.getDataList()
          }
        })
      }).catch(() => {
      })
    }
  }
}
</script>
